package cn.neusoft.controller;

import cn.neusoft.domain.Allocate;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.AllocationQuery;
import cn.neusoft.service.AllocationService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * @Description:调拨单的增删改查
 * @Author:邢旭升
 * @Date:2023/6/28 下午 02:49
 **/
@Api(value = "调拨单管理模块的API", description = "增删改查")
@RestController
@RequestMapping(value = "/allocate",produces = "application/json;charset=utf-8")
public class AllocationController {
    @Autowired
    AllocationService allocationService;



    @PostMapping("/Add")
    public ResultBean AddAllocate(@RequestBody  Allocate allocate){
        if(Objects.equals(allocate.getOut_id(), allocate.getIn_id())){
            return ResultBean.error(AppExceptionCodeMsg.AllocationWarehouseError);
        }

        allocationService.addAllocate(allocate);
        return ResultBean.success();
    }
    @PostMapping("/AddAnyway")
    public ResultBean AddAllocateAnyway(@RequestBody Allocate allocate){
        allocationService.AddAllocateAnyway(allocate);
        return ResultBean.success();
    }

    @PostMapping("/update")
    public ResultBean updateAllocate(@RequestBody Allocate allocate){
        allocate.setUpdate_time(new Date(System.currentTimeMillis()));
        allocationService.updateAllocate(allocate);

        return ResultBean.success();
    }
    @GetMapping("/delete/{allocate_id}")
    public ResultBean deleteAllocate(@PathVariable("allocate_id") String allocate_id){
        allocationService.deleteByAllocateId(allocate_id);
        return ResultBean.success();
    }
    @PostMapping("/getByPage")
    public ResultBean FindAllocateByPages(@RequestBody AllocationQuery query){

        PageBean<Allocate> pageBean = allocationService.findBypage(query);
        return ResultBean.success(pageBean);
    }

    @PostMapping("/getByCondition")
    public ResultBean getByCondition(@RequestBody AllocationQuery query){
        PageBean<Allocate> pageBean = allocationService.findAllByCondition(query);
        return ResultBean.success(pageBean);
    }

    @GetMapping("/getById/{id}")
    public ResultBean FindByAllocateId(@PathVariable("id") String allocate_id){
        Allocate byAllocateId = allocationService.findByAllocateId(allocate_id);
        return ResultBean.success(byAllocateId);
    }

    /**
     * @Description:做完了
     * @Author: 邢旭升
     * @date: 2023/7/13 下午 12:57
     **/
    @ApiOperation(value = "查找已经退回中央仓库的调拨单")
    @PostMapping("/findReturnedAllocate")
    ResultBean findReturnedAllocate(@RequestBody AllocationQuery query){
        PageBean<Allocate> pageBean = allocationService.findReturnedAllocate(query);
        return ResultBean.success(pageBean);
    }
}
